<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

$this->useClass("Action_Community_FeedHandler");

class Action_Community_Search extends Action_Community_FeedHandler {
	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderItems");

		return new AggregatedModel_Feed_ReaderItems();
	}

	//	protected function init() {
	//		parent::init();
	//	}

	public function process() {
		if ( isset($this->aa->template_data['community']) ) {
			$view = $this->rh->views->findByName('search_global');
		} else {
			$view = $this->rh->views->findByName('search_node');
		}
		$view->data = $this->aa->template_data;
		$view->data['aa'] = $this->aa->acl_data;
		
		if ( isset($this->rh->REQUEST) && !empty($this->rh->REQUEST)
		&& isset($this->rh->REQUEST['sstring']) && !empty($this->rh->REQUEST['sstring']) ) {
			$sstring = $this->rh->REQUEST['sstring'];
			$lpage = 0;
			if ( isset($this->rh->REQUEST['lpage']) && !empty($this->rh->REQUEST['lpage']) ) {
				$lpage = abs($this->rh->REQUEST['lpage']+0);
			}
			if ( isset($sstring) && !empty($sstring) ) {
				if ( isset($this->rh->REQUEST['search_options']) &&
				'user_search' == $this->rh->REQUEST['search_options'] ) {
					$smodel = $this->ctx->mm->getModel("SearchUser");
					
					$smodel->setSearchString($sstring);
					$smodel->setLPage($lpage);
					
					$view->data['user_items'] = $smodel->getItems();
					
					if ( empty($view->data['user_items']) ) {
						$view->data['not_found'] = true;
					}
				} else {
					$smodel = $this->ctx->mm->getModel("Search");
					
					$smodel->setSearchString($sstring);
					$smodel->setLPage($lpage);
					$smodel->setSearchInComments($this->rh->search_in_comments);
					
					if ( isset($this->rh->REQUEST['tag']) && !empty($this->rh->REQUEST['tag']) ) {
						$smodel->setTag($this->rh->REQUEST['tag']);
					}
					
					if ( isset($this->rh->REQUEST['tags']) && !empty($this->rh->REQUEST['tags']) ) {
						require_once 'includes/tags.php';
						foreach ( tags_split($this->rh->REQUEST['tags']) as $tag ) {
							$smodel->setTag($tag);
						}
					}
					
					$view->data['items'] = $smodel->getItems();
					$view->data['tags'] = $smodel->getTags();
					
					if ( empty($view->data['items']) ) {
						$view->data['not_found'] = true;
					}
					$view->data['founded_tags'] = $smodel->getFoundedTags();
				}
				
				$lpage = $smodel->getLPage();
				if ( $lpage > 1 ) {
					$view->data['prev_page'] = $lpage - 1;
				}
				$pages = $smodel->getPages();
				if ( $pages > $lpage ) {
					$view->data['next_page'] = $lpage + 1;
				}
				
				$view->data['sstring'] = mb_ereg_replace( ' ', '+', $sstring );
				$view->data['sstring_encode'] = htmlspecialchars( $sstring );
			}
		}
		
		if ( isset($this->rh->REQUEST['search_options']) && !empty($this->rh->REQUEST['search_options']) ) {
			$view->data[$this->rh->REQUEST['search_options']] = true;
			$view->data['search_options'] = $this->rh->REQUEST['search_options'];
		} else {
			$view->data['search_options'] = 'global_search';
		}
		
		if ( isset($view->data['aa']['tdi_url']) ) {
			$view->data['current_tdi']['url'] = $view->data['aa']['tdi_url'];
		}
		
		if ( !empty($view->data['items'])
		&& empty($this->rh->REQUEST['search_options'])
		|| 'global_search' == $this->rh->REQUEST['search_options'] ) {
			$words = $smodel->getWords();
			
			$text = $view->getContent();
			
			if ( mb_eregi( '<div\s+id\=\"search_content\">(.*)</div>\s+<div\s+id\=\"search_content_end\">', $text, $matches )
			&& isset($matches[1]) && !empty($matches[1]) ) {
				$data = $matches[1];
				
				foreach ( $words as $word=>$val ) {
					$data = mb_eregi_replace( "(\>|\b|\s|[^\<\=])([^\<\w\/\;])({$word})([^\=\w\/])(\<|\b|\s|[^\=])", '\1\2<em class="marked-">\3</em>\4\5', $data );
					
					/**
					 * не робит mb_eregi с большими буквами, добавим пару костылей
					 **/
					
					$word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_strtolower(mb_substr($word, 1));
					$data = mb_eregi_replace( "(\>|\b|\s|[^\<\=])([^\<\w\/\;])({$word})([^\=\w\/])(\<|\b|\s|[^\=])", '\1\2<em class="marked-">\3</em>\4\5', $data );
					
					$word = mb_strtoupper($word);
					$data = mb_eregi_replace( "(\>|\b|\s|[^\<\=])([^\<\w\/\;])({$word})([^\=\w\/])(\<|\b|\s|[^\=])", '\1\2<em class="marked-">\3</em>\4\5', $data );
				}
				$text = mb_eregi_replace( '(<div\s+id\=\"search_content\">)(.*)(</div>\s+<div\s+id\=\"search_content_end\">)', "\\1{$data}\\3", $text );
			}
			
			print $text;exit;
		} else {
			return $this->rh->response->setContent( $view );
		}
	}
}